package com.heima.userbehavior.service.impl;

import com.heima.common.constants.BehaviorConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.LikeDto;
import com.heima.model.behavior.dtos.behaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.userbehavior.interceptor.AppUserIdHolder;
import com.heima.userbehavior.service.BehaviorService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @创建人: stone
 * @描述:
 */
@Service
@Transactional
@Slf4j
public class BehaviorServiceImpl implements BehaviorService {

    @Autowired
    private CacheService cacheService;

    /**
     * 文章点赞与取消点赞
     *
     * @param behaviorDto
     * @return
     */
    @Override
    public ResponseResult likesBehavior(behaviorDto behaviorDto) {
        log.info("文章点赞操作方法参数:" + behaviorDto);
        //判断参数
        if (behaviorDto.getArticleId() == 0 || behaviorDto.getType() != 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数错误");
        }
        //执行逻辑
        Integer operation = behaviorDto.getOperation();
        try {
            if (operation == 0) {
                //点赞.添加数据
                cacheService.sAdd(BehaviorConstants.BEHAVIOR_LIKE_IT + behaviorDto.getArticleId(), AppUserIdHolder.get());
            } else if (operation == 1) {
                //取消点赞,删除数据
                cacheService.sRemove(BehaviorConstants.BEHAVIOR_LIKE_IT + behaviorDto.getArticleId(), AppUserIdHolder.get());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.errorResult(405, "无效操作");
        }
        //返回结果
        return ResponseResult.okResult("ok");

    }

    /**
     * 阅读量
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult readBehavior(behaviorDto dto) {
        log.info("文章阅读量操作方法参数:" + dto);
        //判断参数
        if (dto.getArticleId() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数错误");
        }
        //执行操作
        try {
            cacheService.incrBy(BehaviorConstants.BEHAVIOR_READ + dto.getArticleId(), 1);
        } catch (Exception e) {
            return ResponseResult.errorResult(405, "无效操作");
        }
        //返回结果
        return ResponseResult.okResult("ok");
    }

    /**
     * 是否喜欢操作
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult unLikesBehavior(LikeDto dto) {
        log.info("文章是否喜欢操作方法参数:" + dto);
        //判断参数
        if (dto.getArticleId() == 0 || dto.getType() > 1 || dto.getType() < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID, "参数错误");
        }
        //执行逻辑
        Integer operation = dto.getType();
        try {
            if (operation == 0) {
                //不喜欢.添加数据
                cacheService.sAdd(BehaviorConstants.BEHAVIOR_DISLIKE + dto.getArticleId(), AppUserIdHolder.get());
            } else if (operation == 1) {
                //取消不喜欢,删除数据
                cacheService.sRemove(BehaviorConstants.BEHAVIOR_DISLIKE + dto.getArticleId(), AppUserIdHolder.get());
            }
        } catch (Exception e) {
            return ResponseResult.errorResult(405, "无效操作");
        }
        //返回结果
        return ResponseResult.okResult("ok");
    }
}
